package com.yellowpage.bigdata.action;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.opensymphony.xwork2.ActionContext;
import com.yellowpage.white.common.Configuration;
import com.yellowpage.white.common.Constants;
import com.yellowpage.white.struts.BaseAction;
import com.yellowpage.white.struts.Module;
import com.yellowpage.white.struts.OperationType;
import com.yellowpage.white.util.Util4Java;
import com.yellowpage.bigdata.data.NspFatallogImeiMonthregionSum;
import com.yellowpage.bigdata.service.NspFatallogImeiDayregionSumService;
import com.yellowpage.bigdata.service.NspFatallogImeiMonthregionSumService;
import com.yellowpage.bigdata.service.NspFatallogRequsetSumService;
import com.yellowpage.ca.data.User;
import com.yellowpage.ca.service.RegionService;

public class NspFatallogImeiMonthregionSumAction extends BaseAction {

	public String RESULT_QUERY = "query";
	public String RESULT_ADD = "add";
	public String RESULT_EDIT = "edit";

	private Configuration configuration;

	public void setConfiguration(Configuration configuration) {
		this.configuration = configuration;
	}

	private List<NspFatallogImeiMonthregionSum> nspFatallogImeiMonthregionSumList = new ArrayList<NspFatallogImeiMonthregionSum>();
	private List<Map> resultList = new ArrayList<Map>();
	private RegionService regionService;
	private NspFatallogImeiMonthregionSum nspFatallogImeiMonthregionSum;
	private NspFatallogImeiMonthregionSumService nspFatallogImeiMonthregionSumService;
	private NspFatallogImeiMonthregionSumService nspFatallogImeiMonthregionSumService2;
	private NspFatallogRequsetSumService nspFatallogRequsetSumService;
	private NspFatallogImeiDayregionSumService nspFatallogImeiDayregionSumService;
	private String selectedId;
	private String[] selectedIds;
	private String message;
	private Map requestTypeMap = new HashMap();
	private String query_fatallogImeiMonthregionSumId;
	private String query_requestType;
	private String query_imeiAllCount;
	private String query_imeiNewCount;
	private String query_imeiMonthCount;
	private String query_createUser;
	private String query_regionCode;
	private String query_createTime;
	private String query_note;
	private String query_logMonth;
	private String query_logDate_end;
	private String query_logDate;
	private String query_requestCount;
	private String query_daraFlag;
	private String query_requestNoIMEICount;
	private String query_requestIMEICount;
	private String channelCode;
	private String regionName;
	private Map<String, String> channelMap = new HashMap<String, String>();
	private Map<String, String> requestTypeList;
	private Map<String, String> regionMap;

	public RegionService getRegionService() {
		return regionService;
	}

	public void setRegionService(RegionService regionService) {
		this.regionService = regionService;
	}

	public String getRegionSql() {
		Map session = ActionContext.getContext().getSession();
		User user = (User) session.get("userObject");
		String function_code = "OMC_ABILITYCALL_LOG_COUNT_MONTH_REGIONCOUNT";
		String regionsql = regionService.getRegionclauseByUserFunction(
				user.getUserId(), function_code, "REGION_CODE");
		return regionsql;
	}

	public void prepare() {
		super.prepare();
		//
		requestTypeMap = nspFatallogImeiDayregionSumService.getrequestTypeMap();
		if (this.selectedId != null && !this.selectedId.equals("")) {
			this.nspFatallogImeiMonthregionSum = nspFatallogImeiMonthregionSumService
					.getNspFatallogImeiMonthregionSum(Long
							.parseLong(selectedId));
		}
	}

	public String queryByPage() {
		initPageRender();

		Map<String, String> queryMap = new HashMap<String, String>();
		channelMap = nspFatallogRequsetSumService.getChannelMap();
		queryMap.clear();
		String str = "";
		if (!channelCode.equals("") && query_requestType.equals("")) {
			String val[] = channelCode.split(";");
			for (String ss : val) {
				for (String key : channelMap.keySet()) {
					if (ss.equals(channelMap.get(key))) {
						str += "'" + key + "',";
					}
				}
			}
			str = str.substring(0, str.length() - 1);
			queryMap.put("requestType",
					nspFatallogRequsetSumService.getrequestTypeString(str));
		} else if (!query_requestType.equals("")) {
			requestTypeList = nspFatallogRequsetSumService.getrequestTypeMap();
			String[] val = query_requestType.split(";");
			for (String str1 : val) {
				for (String ss : requestTypeList.keySet()) {
					if (requestTypeList.get(ss).contains(str1)) {
						str += "'" + ss + "',";
						break;
					}
				}
			}
			str = str.substring(0, str.length() - 1);
			queryMap.put("requestType", str);
		} else {
			queryMap.put("requestType", query_requestType);
		}
		// 地区
		if (!regionName.equals("")) {
			regionMap = nspFatallogRequsetSumService.getRegionMap(getRegionSql());
			String[] val = regionName.split(";");
			str = "";
			for (String ss : val) {
				for (String key : regionMap.keySet()) {
					if (regionMap.get(key).contains(ss)) {
						str += "'" + key + "',";
						break;
					}
				}
			}
			queryMap.put("regionCode", str.substring(0, str.length() - 1));
		} else {
			queryMap.put("regionCode", regionName);
		}
		queryMap.put("logDate", query_logDate);
		queryMap.put("query_logDate_end", query_logDate_end);

		resultList = nspFatallogImeiMonthregionSumService
				.queryNspFatallogImeiMonthregionSumByCondition(queryMap,
						pageRender);

		// resultList =
		// nspFatallogImeiMonthregionSumService.queryNspFatallogImeiMonthregionSumByCondition(queryMap,
		// pageRender);
		// resultList =
		// nspFatallogImeiMonthregionSumService.queryNspFatallogImeiMonthregionSumByPage(queryMap,
		// pageRender);

		setPageRenderToRequest();

		return RESULT_QUERY;
	}

	public String toAdd() {
		return RESULT_ADD;
	}

	public String save() {

		nspFatallogImeiMonthregionSumService
				.saveNspFatallogImeiMonthregionSum(nspFatallogImeiMonthregionSum);

		message = "新增成功!";
		return RESULT_ADD;
	}

	public void validateSave() {
		if (nspFatallogImeiMonthregionSum.getRequestType().length() == 0) {
			this.addActionError("** RequestType 不能为空 !");
		}
		if (nspFatallogImeiMonthregionSum.getRegionCode().length() == 0) {
			this.addActionError("** RegionCode 不能为空 !");
		}
		if (nspFatallogImeiMonthregionSum.getNote().length() == 0) {
			this.addActionError("** Note 不能为空 !");
		}
		if (nspFatallogImeiMonthregionSum.getLogMonth().length() == 0) {
			this.addActionError("** LogMonth 不能为空 !");
		}

		this.setValidatorResult("/bigdata/NspFatallogImeiMonthregionSumAdd.jsp");
	}

	public String toEdit() {
		Long id = new Long(selectedId);

		nspFatallogImeiMonthregionSum = nspFatallogImeiMonthregionSumService
				.getNspFatallogImeiMonthregionSum(id);

		return RESULT_EDIT;
	}

	public String update() {

		nspFatallogImeiMonthregionSumService
				.updateNspFatallogImeiMonthregionSum(nspFatallogImeiMonthregionSum);

		message = "修改成功!";

		return RESULT_EDIT;
	}

	public void validateUpdate() {
		if (nspFatallogImeiMonthregionSum.getRequestType().length() == 0) {
			this.addActionError("** RequestType 不能为空 !");
		}
		if (nspFatallogImeiMonthregionSum.getRegionCode().length() == 0) {
			this.addActionError("** RegionCode 不能为空 !");
		}
		if (nspFatallogImeiMonthregionSum.getNote().length() == 0) {
			this.addActionError("** Note 不能为空 !");
		}
		if (nspFatallogImeiMonthregionSum.getLogMonth().length() == 0) {
			this.addActionError("** LogMonth 不能为空 !");
		}
		this.setValidatorResult("/bigdata/NspFatallogImeiMonthregionSumUpdate.jsp");
	}

	public String delete() {
		List ids = new ArrayList();

		for (String id : selectedIds) {
			ids.add(Long.valueOf(id));
		}

		try {
			this.nspFatallogImeiMonthregionSumService.delete(ids);
		} catch (Exception e) {
			System.out.println("异常....");
			this.addActionError("** 可能含有子记录约束冲�?删除失败 !");
		}

		return queryByPage();
	}

	/**
	 * getters and setters
	 * 
	 * @return
	 */
	public void setNspFatallogImeiMonthregionSum(
			NspFatallogImeiMonthregionSum nspFatallogImeiMonthregionSum) {
		this.nspFatallogImeiMonthregionSum = nspFatallogImeiMonthregionSum;
	}

	public NspFatallogImeiMonthregionSum getNspFatallogImeiMonthregionSum() {
		return nspFatallogImeiMonthregionSum;
	}

	public void setNspFatallogImeiMonthregionSumService(
			NspFatallogImeiMonthregionSumService nspFatallogImeiMonthregionSumService) {
		this.nspFatallogImeiMonthregionSumService = nspFatallogImeiMonthregionSumService;
	}

	public List<NspFatallogImeiMonthregionSum> getNspFatallogImeiMonthregionSumList() {
		return nspFatallogImeiMonthregionSumList;
	}

	public void setNspFatallogImeiMonthregionSumList(
			List<NspFatallogImeiMonthregionSum> nspFatallogImeiMonthregionSumList) {
		this.nspFatallogImeiMonthregionSumList = nspFatallogImeiMonthregionSumList;
	}

	public String getSelectedId() {
		return selectedId;
	}

	public void setSelectedId(String selectedId) {
		this.selectedId = selectedId;
	}

	public String[] getSelectedIds() {
		return selectedIds;
	}

	public void setSelectedIds(String[] selectedIds) {
		this.selectedIds = selectedIds;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public List<Map> getResultList() {
		return resultList;
	}

	public void setResultList(List<Map> resultList) {
		this.resultList = resultList;
	}

	public String getQuery_fatallogImeiMonthregionSumId() {
		return this.query_fatallogImeiMonthregionSumId;
	}

	public void setQuery_fatallogImeiMonthregionSumId(
			String fatallogImeiMonthregionSumId) {
		this.query_fatallogImeiMonthregionSumId = fatallogImeiMonthregionSumId;
	}

	public String getQuery_requestType() {
		return this.query_requestType;
	}

	public void setQuery_requestType(String requestType) {
		this.query_requestType = requestType;
	}

	public String getQuery_imeiAllCount() {
		return this.query_imeiAllCount;
	}

	public void setQuery_imeiAllCount(String imeiAllCount) {
		this.query_imeiAllCount = imeiAllCount;
	}

	public String getQuery_imeiNewCount() {
		return this.query_imeiNewCount;
	}

	public String getQuery_requestCount() {
		return query_requestCount;
	}

	public void setQuery_requestCount(String requestCount) {
		this.query_requestCount = requestCount;
	}

	public String getQuery_daraFlag() {
		return query_daraFlag;
	}

	public void setQuery_daraFlag(String dataFalg) {
		this.query_daraFlag = dataFalg;
	}

	public void setQuery_imeiNewCount(String imeiNewCount) {
		this.query_imeiNewCount = imeiNewCount;
	}

	public String getQuery_imeiMonthCount() {
		return this.query_imeiMonthCount;
	}

	public void setQuery_imeiMonthCount(String imeiMonthCount) {
		this.query_imeiMonthCount = imeiMonthCount;
	}

	public String getQuery_createUser() {
		return this.query_createUser;
	}

	public void setQuery_createUser(String createUser) {
		this.query_createUser = createUser;
	}

	public String getQuery_regionCode() {
		return this.query_regionCode;
	}

	public void setQuery_regionCode(String regionCode) {
		this.query_regionCode = regionCode;
	}

	public String getQuery_createTime() {
		return this.query_createTime;
	}

	public void setQuery_createTime(String createTime) {
		this.query_createTime = createTime;
	}

	public String getQuery_note() {
		return this.query_note;
	}

	public void setQuery_note(String note) {
		this.query_note = note;
	}

	public String getQuery_logMonth() {
		return this.query_logMonth;
	}

	public void setQuery_logMonth(String logMonth) {
		this.query_logMonth = logMonth;
	}

	public NspFatallogImeiDayregionSumService getNspFatallogImeiDayregionSumService() {
		return nspFatallogImeiDayregionSumService;
	}

	public void setNspFatallogImeiDayregionSumService(
			NspFatallogImeiDayregionSumService nspFatallogImeiDayregionSumService) {
		this.nspFatallogImeiDayregionSumService = nspFatallogImeiDayregionSumService;
	}

	public NspFatallogImeiMonthregionSumService getNspFatallogImeiMonthregionSumService() {
		return nspFatallogImeiMonthregionSumService;
	}

	public Map getRequestTypeMap() {
		return requestTypeMap;
	}

	public void setRequestTypeMap(Map requestTypeMap) {
		this.requestTypeMap = requestTypeMap;
	}

	public String getQuery_logDate_end() {
		return query_logDate_end;
	}

	public void setQuery_logDate_end(String query_logDate_end) {
		this.query_logDate_end = query_logDate_end;
	}

	public String getQuery_logDate() {
		return query_logDate;
	}

	public void setQuery_logDate(String query_logDate) {
		this.query_logDate = query_logDate;
	}

	public String getQuery_requestNoIMEICount() {
		return query_requestNoIMEICount;
	}

	public void setQuery_requestNoIMEICount(String requestNoIMEICount) {
		this.query_requestNoIMEICount = requestNoIMEICount;
	}

	public String getQuery_requestIMEICount() {
		return query_requestIMEICount;
	}

	public void setQuery_requestIMEICount(String requestIMEICount) {
		this.query_requestIMEICount = requestIMEICount;
	}

	public NspFatallogRequsetSumService getNspFatallogRequsetSumService() {
		return nspFatallogRequsetSumService;
	}

	public void setNspFatallogRequsetSumService(
			NspFatallogRequsetSumService nspFatallogRequsetSumService) {
		this.nspFatallogRequsetSumService = nspFatallogRequsetSumService;
	}

	public String getChannelCode() {
		return channelCode;
	}

	public void setChannelCode(String channelCode) {
		this.channelCode = channelCode;
	}

	public String getRegionName() {
		return regionName;
	}

	public void setRegionName(String regionName) {
		this.regionName = regionName;
	}

	public Map<String, String> getChannelMap() {
		return channelMap;
	}

	public void setChannelMap(Map<String, String> channelMap) {
		this.channelMap = channelMap;
	}

	public Map<String, String> getRequestTypeList() {
		return requestTypeList;
	}

	public void setRequestTypeList(Map<String, String> requestTypeList) {
		this.requestTypeList = requestTypeList;
	}

	public Map<String, String> getRegionMap() {
		return regionMap;
	}

	public void setRegionMap(Map<String, String> regionMap) {
		this.regionMap = regionMap;
	}

}
